import QuestSpawn from "../QuestSpawn";
import ServerCenter from '../../Server/ServerCenter';
import GameUtil from '../../Fantasy/Util/GameUtil';
import LevelInfoCard from "../PrefabJs/LevelInfoCard";
import LoadUtil from "../../Fantasy/Util/LoadUtil";
import GameConfig, { StarType } from "../../Server/Data/GameConfig";
import SoundUtil from "../../Fantasy/Util/SoundUtil";
import PromptManage from "../PromptManage";
import JsbUtil from "../../Fantasy/Util/JsbUtil";

const {ccclass, property} = cc._decorator;
const nameArr = ["双子座","双鱼座","处女座","天秤座","天蝎座","射手座","巨蟹座","摩羯座","水瓶座","狮子座","白羊座","金牛座"]
@ccclass
export default class GameSceneJs extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    @property(cc.Node)
    questNumNode:cc.Node = null;
    @property(cc.Node)
    TimeNode: cc.Node = null;
    @property(cc.Node)
    expNumNode: cc.Node = null;
    @property(cc.Node)
    lifeNumNode: cc.Node = null;
    @property(cc.Node)
    questNode:cc.Node = null;
    @property(cc.Prefab)
    selectQuest: cc.Prefab = null;
    @property(cc.Prefab)
    matchQuest: cc.Prefab = null;
    @property(cc.Prefab)
    WriteQuest: cc.Prefab = null;
    @property(cc.Prefab)
    wrongPre: cc.Prefab = null;
    @property(cc.Prefab)
    cardPre: cc.Prefab = null;
    @property(cc.Node)
    rushFail:cc.Node = null;
    @property(cc.Node)
    rushSuccess:cc.Node = null;
    @property(cc.Node)
    top:cc.Node = null;
    @property(cc.Node)
    addExpNode:cc.Node = null;
    @property(cc.Node)
    wrongButton:cc.Node = null;
    @property(cc.Node)
    openButton:cc.Node = null;
    @property(cc.Node)
    continueButton:cc.Node = null;
    @property(cc.Node)
    jiangli:cc.Node = null;
    wrongTime;
    questArr
    life;
    totalQuestNum;
    currQuestNum;
    expNum;
    dt;
    inTime;
    bookid;
    unit;
    part: any;
    firstName;
    onLoad () {
        SoundUtil.stopMusic();
        
        this.dt = 0;
        this.expNum = 0;
        this.life = 5;//生命值
        this.lifeNumNode.getChildByName("Label").getComponent(cc.Label).string = this.life;
        this.expNumNode.getChildByName("Label").getComponent(cc.Label).string = this.expNum;
        this.currQuestNum = 0;//当前题目数
        this.inTime = new Date().getTime();
        QuestSpawn.setIsContest(false);
        QuestSpawn.setIsWrong(false);
        JsbUtil.listenAndroidBack();

    }
    updateExp(number){
        this.expNum = parseInt(this.expNum) + parseInt(number);
        this.expNumNode.getChildByName("Label").getComponent(cc.Label).string = this.expNum+"";
        
    }
    updateQuestNum(){
        this.questNumNode.getComponent(cc.Label).string = this.currQuestNum +"/"+this.totalQuestNum +"";
    }
    updateLife(){
        //当前题目错了
        this.wrongTime ++;
        this.life --;
        if(this.life < 0){
            let quest = this.questNode.children;
            for(let node of quest){
                node.active = false;
            }
            this.gameOver();
            return;
        }else if(this.wrongTime == 1){
            let quest = this.questNode.children;
            for(let i in quest){
                QuestSpawn.wrongArr.push(quest[i]);
                Log(QuestSpawn.wrongArr);
            }
        }
        this.lifeNumNode.getChildByName("Label").getComponent(cc.Label).string = this.life;
    }
    initBookUnitPart(book,unit,part){
        this.bookid = book;
        this.unit = unit;
        this.part = part;
        QuestSpawn.getWordsInfo(book,unit);
        this.totalQuestNum = QuestSpawn.questNum;
        this.initQuest(book,unit);
    }
    initQuest(book,unit){
        this.wrongTime = 0;
        let quest = this.questNode.children;
        for(let node of quest){
            if (QuestSpawn.wrongArr.indexOf(node) < 0){
                node.destroy();
            }
            else{
                node.active = false;
                node.parent = null;
            }
        }

        this.currQuestNum ++;
        Log("题目数：",this.currQuestNum);
        this.updateQuestNum();
        if(this.currQuestNum == this.totalQuestNum + 1){
            this.top.active = false;
            this.gameSuccess()
            Log("游戏结束，加载闯关成功预制");
            ServerCenter.addUserExp(this.expNum);
            ServerCenter.setUserRushLevel(this.unit,this.part,this.bookid);
            // ServerCenter.setUserRushLevel(8,this.part,this.bookid);
            return;
        }

        if(book && unit){
            this.questArr= QuestSpawn.getWordsInfo(book,unit);
            QuestSpawn.wrongArr = [];
        }

        let pross = this.currQuestNum/this.totalQuestNum;
        let random = Math.round(Math.random()*20);
        if(pross < 0.85){
            let node = cc.instantiate(this.selectQuest);
            node.getComponent("SelectQuestJs").init(this,this.questArr,this.bookid,this.unit);
            node.parent = this.questNode;
        }else if(this.unit > 1 && random >16){
            let node = cc.instantiate(this.WriteQuest);
            node.getComponent("WriteQuestJs").init(this,this.questArr);
            node.parent = this.questNode;
        }else if(pross >= 0.85){
            let node = cc.instantiate(this.matchQuest);
            node.getComponent("MacthQuestJs").init(this,this.questArr);
            node.parent = this.questNode;

        }

    }
    gameOver(){
        let quest = this.questNode.children;
        this.top.active = false;
        this.rushFail.active = true;
    }


    gameSuccess(){
        SoundUtil.playEffect("music/success")
        let quest = this.questNode.children;
        for(let node of quest){
            node.active = false;
        }
        this.rushSuccess.active = true;
        this.addExpNode.getComponent(cc.Label).string = this.expNum;
        if(QuestSpawn.wrongArr.length == 0){
            this.wrongButton.active = false;
        }
         
    }
    openBox(){
        SoundUtil.playEffect("music/click");
        this.openButton.active = false;
        this.jiangli.active = false;
        this.continueButton.active = true;
        let random = Math.round(Math.random()*48);
        let level = Math.floor(random / 12);
        level = level+1;
        Log("dengji ",level);
        let  card = cc.instantiate(this.cardPre);
        let type = random % 12;
        card.getComponent("StarCard").setStarCard(type,level,false);
        ServerCenter.GetCard(type);
        card.parent = this.rushSuccess;
    }
    computerNextDate(){
        let currDate = new Date();
        let mydate = currDate.getTime();
        let gap = Math.floor((mydate - this.inTime)/1000); //单位是毫秒 除以1000获得 已经过去了几秒
        let allTime = 6*60-1 ;
        let needTime = allTime - gap;
        if (needTime<0){
            needTime = 0;
        }
        this.TimeNode.getChildByName("Label").getComponent(cc.Label).string = this.showCountdown(needTime);
        return needTime;
        
    }
    wrong(){
        SoundUtil.playEffect("music/click");
        this.wrongButton.active = false;
        let wrongNode = cc.instantiate(this.wrongPre)
        wrongNode.parent = this.node;
    }

    showCountdown(time){
        let str = '';
        let minStr = '';
        let secStr = '';
        let minute = Math.floor(time/60);
        let second = time%60;
        if(minute >= 10)
            minStr = '' + minute;
        else{
            minStr = '0' + minute;
        }
        if(second >= 10){
            secStr = '' + second;
        }else{
            secStr = '0' + second;
        }
        str = minStr + ':' + secStr;
        return str;
    }
    update(dt){
        this.dt += dt;
        if (this.dt>=1){
            this.dt = 0;
            let t = this.computerNextDate();
            if (t == 0){
                this.gameOver();
            }
        }
    }
    btnCallback(target,data){
        SoundUtil.playEffect("music/click");
        if(data == "close"){

            PromptManage.addExitDialog(false)

        }else if(data == "continue"){
            GameUtil.gotoScene("MainScene",()=>{
                cc.find("Canvas").getComponent("MainScene").btnCallback(null,"Rush");
            },true);
        }
    }
 



}
